package com.miniot.base.controller;


import com.miniot.business.base.BaseController;
import com.miniot.business.base.PageR;
import com.miniot.business.base.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miniot.base.entity.Product;
import com.miniot.base.vo.ProductVO;
//import com.miniot.base.dto.ProductDTO;
import com.miniot.base.service.ProductService;
import com.miniot.business.database.mybatis.conditions.query.LbqWrapper;
import com.miniot.business.database.mybatis.conditions.Wraps;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 产品表(Product)表控制层
 *
 * @author mikelei
 * @since 2022-12-20 18:33:46
 * v1.0.12 生成
 */

@Slf4j
@Api(value = "product", tags = "产品表")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;

    /**
     * 分页查询所有数据
     *
     * @param product 查询实体
     *                current  当前页
     *                size  每页显示几条
     * @return 分页所有数据
     */
    @GetMapping
    @ApiOperation(value = "分页查询产品表信息", notes = "分页查询产品表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),})
    public PageR selectAll(ProductVO product) {
        Page<Product> page = getPage();
        LbqWrapper<Product> wrapper = Wraps.lbQ();
        wrapper
                //.like(Product::getName, product.getName())
                //.eq(Product::getId, product.getId())
                .geHeader(Product::getCreateTime, product.getStartTime())
                .leFooter(Product::getCreateTime, product.getEndTime())
                .orderByDesc(Product::getCreateTime);

        this.productService.page(page, wrapper);
        return PageR.success(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "查询产品表信息", notes = "查询产品表信息")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.productService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param product 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增产品表信息", notes = "查询产品表信息不为空的字段")
    public R insert(@RequestBody Product product) {
        return success(this.productService.save(product));
    }

    /**
     * 修改数据
     *
     * @param product 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "修改产品表信息", notes = "修改产品表信息不为空的字段")
    public R update(@RequestBody Product product) {
        return success(this.productService.updateById(product));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除产品表信息", notes = "根据id物理删除产品表信息")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.productService.removeByIds(idList));
    }

    @GetMapping("/test")
    @PreAuthorize( "hasAuthority('API-PRODUCT-TEST')")
    @ApiOperation(value = "删除产品表信息", notes = "根据id物理删除产品表信息")
    public R test() {
        return success("ok");
    }
}

